package com.cooper.resource.download;

import java.io.File;
import java.io.Serializable;
import java.util.Collection;

import com.cooper.android.comm.FileUtils;
import com.cooper.resource.download.DownloadManagerService.DownloadBinder;
import com.cooper.scraper.R;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public abstract class DownloadingActivity extends ListActivity implements
		DownloadControl {

	public static final String ADD_DOWN_PARAM_NAME = "ADD_DOWN_PARAM_NAME";

	private DownloadBinder binder = null;

	protected ServiceConnection serviceConn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			binder = (DownloadBinder) service;

			adapter = new DownloadItemAdapter(DownloadingActivity.this,
					DownloadingActivity.this);
			setListAdapter(adapter);
			binder.setDownloadListener(adapter);

			if (waitDownRes != null) {
				addDownload(waitDownRes);
				waitDownRes = null;
			} else
				onItemCountChange(adapter.getCount());

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			binder.setDownloadListener(null);
			binder = null;
		}

	};

	protected abstract void onCompleteDown(DownloadStatus res);

	protected abstract void onItemCountChange(int count);

	protected abstract void onItemCancel(DownloadStatus res);

	protected abstract void onCancelAll();

	protected boolean completeDown(DownloadStatus res) {
		if (binder.completeMove(res)) {
			adapter.removeItem(res);
			onItemCountChange(adapter.getCount());
			return true;
		}
		return false;
	}

	protected void doItemCancel(DownloadStatus res) {
		binder.cancel(res);
		adapter.removeItem(res);
		onItemCountChange(adapter.getCount());
	}

	protected void doCancelAllItem() {
		binder.cancelAll();
		adapter.removeAll();
		onItemCountChange(adapter.getCount());
	}

	@Override
	public void pause(DownloadStatus res) {
		binder.pause(res);
	}

	@Override
	public void pauseAll() {
		binder.pauseAll();
	}

	@Override
	public void continueAll() {
		binder.continueAll();
	}

	@Override
	public void cancelAll() {
		onCancelAll();
	}

	@Override
	public void downContinue(DownloadStatus res) {
		binder.downContinue(res);
	}

	@Override
	public void cancel(DownloadStatus res) {
		onItemCancel(res);
	}

	@Override
	public boolean completeMove(DownloadStatus res) {
		if (res.getDownloadResource().isFinish()) {
			onCompleteDown(res);
		}
		return false;
	}

	public Collection<DownloadStatus> getDownloading() {
		return binder.getDownloading();
	}

	// public void onCreate(Bundle savedInstanceState) {
	// super.onCreate(savedInstanceState);
	// setContentView(R.layout.downloadings);

	// bindService(new Intent(this, DownloadManagerService.class),
	// serviceConn, Context.BIND_AUTO_CREATE);

	// }

	private DownloadResource waitDownRes = null;
	private DownloadItemAdapter adapter;

	@Override
	public void onStart() {
		super.onStart();
		Serializable downData = getIntent().getSerializableExtra(
				ADD_DOWN_PARAM_NAME);
		if (downData != null) {
			DownloadResource requestRes = (DownloadResource) downData;
			getIntent().getExtras().remove(ADD_DOWN_PARAM_NAME);
			if (binder != null) {
				addDownload(requestRes);

			} else {
				waitDownRes = requestRes;
			}
		}
	}

	private void addDownload(DownloadResource requestRes) {

		Log.d("DownloadingActivity", requestRes.getDownloadTitle());
		File storeDir = requestRes.getDownloadingSaveDir();
		if (storeDir.exists()) {
			Log.d("DownloadingActivity", "Down is exists");
			if (!binder.downloadPrefIsExists(storeDir)) {
				FileUtils.delete(storeDir);
			} else {
				Toast.makeText(this, R.string.download_task_exists,
						Toast.LENGTH_LONG).show();
				return;
			}
		}
		adapter.addDownload(binder.addDownload(requestRes));
		onItemCountChange(adapter.getCount());

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
